<?php

/**
 * Description of SitesController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Sites\Backend\Controller;

if( !defined('IN') ) die('Hacking Attempt');

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;

class SitesController extends BackController{
    
    protected $siteform;
    protected $infos;
    public function executeSites(HttpRequest $request){
        
        $arraySite = array();
        $this->page->addVar('title', 'Gestion du site');
        
        $manager = $this->managers->getManagerOf('Sites');
        
        $this->siteform = new Form('sitesInfo', 'post');
        
        $this->siteform->action('sites.html');
        $this->siteform->add('Select', 'devise')
                        ->label('Devise')
                        ->choices(array(
                            '&euro;' => '€',
                            '$' => '$',
                        ))
                ->required(true);
        $this->siteform->add('Text', 'nameSites')
                       ->label('Nom du site')
                       ->required(true);
        
        $this->siteform->add('Time', 'heureOuverture')
                       ->label('Heure d\'ouverture')
                       ->required(false);
        
        $this->siteform->add('Time', 'heureFermeture')
                       ->label('Heure de fermeture')
                       ->required(false);
        
        $this->siteform->add('Email', 'emailServClient')
                       ->label('Email Service Client')
                       ->required(true);
        
        $this->siteform->add('Email', 'emailServTech')
                       ->label('Email Service Technique')
                       ->required(true);
        
        $this->siteform->add('Email', 'emailServReclamation')
                       ->label('Email Service Reclamation')
                       ->required(true);
        
        $this->siteform->add('Email', 'emailPaypal')
                       ->label('Email Service Paypal')
                       ->required(true);
        
        $this->siteform->add('Submit', 'submit')->value("Modifier");
        if($request->getMethod('post')){
           //test de la validation du post
            if ($this->siteform->is_valid($_POST)){
                $_POST['isoDevise'] = $_POST['devise']=='$'?'USD':'EUR';
                if($manager->updateSiteInfos($_POST)){
                    $this->infos='Modification effectuée';
                    $this->updateSession($manager);
                }
            }
            
            $this->siteform->bound($_POST);
        }else{
            $this->updateSession($manager);
            $this->siteform->bound($_SESSION['site']);
        } 
        $this->page->addVar('siteForm', $this->siteform);
        $this->page->addVar('infos', $this->infos);
    }
    
    public function updateSession($manager){
        $sites = $manager->getSitesInfos();
            if($sites){
                $_SESSION['site']['nameSites'] = $sites->getNameSites();
                $_SESSION['site']['heureOuverture'] = $sites->getHeureOuverture();
                $_SESSION['site']['heureFermeture'] = $sites->getHeureFermeture();
                $_SESSION['site']['emailServClient'] = $sites->getEmailServClient();
                $_SESSION['site']['emailServTech'] = $sites->getEmailServTech();
                $_SESSION['site']['emailServReclamation'] = $sites->getEmailServReclamation();
                $_SESSION['site']['emailPaypal'] = $sites->getEmailPaypal();
                $_SESSION['site']['devise'] = $sites->getDevise();
                $_SESSION['site']['isoDevise'] = $sites->getIsoDevise();
            }
    }
}

?>
